package com.ngc.mall.common.interceptor;

import com.ngc.mall.common.configure.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//implements WebMvcConfigurer
//extends WebMvcConfigurationSupport
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

//    @Autowired
//    private PreAuthorizeInterceptor preAuthorizeInterceptor;
    @Autowired
    private PreAuthorizeInterceptor preAuthorizeInterceptor(){
        return new PreAuthorizeInterceptor();
    };
//    @Autowired
//    private AuthInterceptor authInterceptor;
//    public void configurePathMatch(PathMatchConfigurer configurer){
//        configurer.addPathPrefix(
//                "/api",
//                (t) -> t.isAnnotationPresent(RestController.class)||t.isAnnotationPresent(Controller.class)
//        );
//    }

//
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(authInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login","/user/test")
                .excludePathPatterns("/websocket","/websocket/**","/myWs","/myWs/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
        registry.addInterceptor(preAuthorizeInterceptor());
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }
}
